package com.kalvin.kvf.modules.dy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.dy.entity.Bloggers;
import com.kalvin.kvf.modules.dy.entity.IntentionCust;
import com.kalvin.kvf.modules.dy.entity.Task;
import com.kalvin.kvf.modules.dy.entity.Video;
import com.kalvin.kvf.modules.dy.mapper.BloggersMapper;
import com.kalvin.kvf.modules.dy.mapper.IntentionCustMapper;
import com.kalvin.kvf.modules.dy.mapper.TaskMapper;
import com.kalvin.kvf.modules.dy.mapper.VideoMapper;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.entity.Cookies;
import com.kalvin.kvf.modules.hxy.mapper.AccountMapper;
import com.kalvin.kvf.modules.hxy.mapper.CookiesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 * @since 2023-11-12 16:33:52
 */
@Service
public class DyCrawlerServiceImpl  implements DyCrawlerService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private CookiesMapper cookiesMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    IntentionCustMapper intentionCustMapper;
    @Autowired
    private AccountMapper accountMapper;

//    @Bean
//    public RestTemplate restTemplate(RestTemplateBuilder builder) {
//        return builder.build();
//    }
    @Autowired
    private RestTemplate restTemplate;

    @Override
    @Async
    public R taskStart() throws Exception {
        Random random = new Random();
        //查询当前用户创建的任务
        Task task = taskMapper.selectOne(new QueryWrapper<Task>().eq("phone", ShiroKit.getUser().getUsername()).eq("task_status", 0).lambda().orderByAsc(Task::getCreateTime).last("limit 1"));
        String encode = URLEncoder.encode(task.getSearchKeyword(), "UTF-8");
        //查询当前登录用户是否设置cookie
        Cookies cookie;
        Cookies phoneCookie = cookiesMapper.selectOne(new QueryWrapper<Cookies>().eq("medium_type", 1).eq("phone", ShiroKit.getUser().getUsername()).lambda().orderByAsc(Cookies::getLastLoginTime).last("limit 1"));
        if (phoneCookie!=null){
            //获取cookie、msToken、xBogus
            cookie = phoneCookie;
        }else {
            cookie = cookiesMapper.selectOne(new QueryWrapper<Cookies>().eq("medium_type", 1).lambda().orderByAsc(Cookies::getLastLoginTime).last("limit 1"));;
        }
        //修改cookie的登录时间
        cookie.setLastLoginTime(LocalDateTime.now());
        cookiesMapper.updateById(cookie);
        //修改任务状态
        task.setTaskStatus(1);
        taskMapper.updateById(task);
        System.out.println(cookie.getCookies());
        int a = 0;
        int pcursor = 10;
        int hasMore = 1;//默认是1
        long time = 0;
        long oneHourLaterTimestamp = 0;
        LocalDateTime now = LocalDateTime.now();
        //1小时后的时间戳
        LocalDateTime oneHourLater = now.plus(Duration.ofHours(1));
        oneHourLaterTimestamp = oneHourLater.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        while (a < task.getCustNum() && hasMore!=0&&time<oneHourLaterTimestamp) {
            int randomNumber = random.nextInt(3000) + 2000;
            Thread.sleep(randomNumber);
            URL url = new URL("https://www.douyin.com/aweme/v1/web/search/item/?device_platform=webapp&aid=6383&channel=channel_pc_web&search_channel=aweme_video_web&sort_type=2&publish_time=0&keyword="+encode+"&search_source=normal_search&query_correct_type=1&is_filter_search=0&from_group_id=&offset="+pcursor+"&count=10&pc_client_type=1&version_code=170400&version_name=17.4.0&cookie_enabled=true&screen_width=2560&screen_height=1440&browser_language=zh-CN&browser_platform=Win32&browser_name=Chrome&browser_version=119.0.0.0&browser_online=true&engine_name=Blink&engine_version=119.0.0.0&os_name=Windows&os_version=10&cpu_core_num=12&device_memory=8&platform=PC&downlink=10&effective_type=4g&round_trip_time=50&webid=7294112732198127143"+"&msToken="+cookie.getMsToken()+"&X-Bogus="+cookie.getXBogus());
            System.out.println(url);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("GET");
            httpConn.setRequestProperty("authority", "www.douyin.com");
            httpConn.setRequestProperty("accept", "application/json, text/plain, */*");
            httpConn.setRequestProperty("accept-language", "zh-CN,zh;q=0.9");
            httpConn.setRequestProperty("cookie", cookie.getCookies());
            httpConn.setRequestProperty("referer", "https://www.douyin.com/search/"+encode+"?publish_time=0&sort_type=0&source=switch_tab&type=video");
            System.out.println(encode);
            httpConn.setRequestProperty("sec-ch-ua", "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"");
            httpConn.setRequestProperty("sec-ch-ua-mobile", "?0");
            httpConn.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
            httpConn.setRequestProperty("sec-fetch-dest", "empty");
            httpConn.setRequestProperty("sec-fetch-mode", "cors");
            httpConn.setRequestProperty("sec-fetch-site", "same-origin");
            httpConn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
            int responseCode = httpConn.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            System.out.println("Response Code: " + responseCode);
            JSONObject jsonObject =  JSON.parseObject(response.toString());
            System.out.println(jsonObject);
            JSONArray dataList = jsonObject.getJSONArray("data");
            for (Object o:dataList){
                try{
                    Video video=new Video();
                    int plCursor =5;
                    int plhasMore = 1;//默认是1
                    while (a < task.getCustNum() && plhasMore!=0){
                        int plrandomNumber = random.nextInt(3000) + 2000;
                        Thread.sleep(plrandomNumber);
                        JSONObject data = JSON.parseObject(o.toString());
                        Object videoId = data.getJSONObject("aweme_info").get("aweme_id");
                        URL url2 = new URL("https://www.douyin.com/aweme/v1/web/comment/list/?device_platform=webapp&aid=6383&channel=channel_pc_web&aweme_id="+videoId+"&cursor="+plCursor+"&count=20&item_type=0&whale_cut_token=&cut_version=1&rcFT=&pc_client_type=1&version_code=170400&version_name=17.4.0&cookie_enabled=true&screen_width=2560&screen_height=1440&browser_language=zh-CN&browser_platform=Win32&browser_name=Chrome&browser_version=119.0.0.0&browser_online=true&engine_name=Blink&engine_version=119.0.0.0&os_name=Windows&os_version=10&cpu_core_num=12&device_memory=8&platform=PC&downlink=10&effective_type=4g&round_trip_time=50&webid=7294112732198127143&msToken="+cookie.getMsToken()+"&X-Bogus="+cookie.getXBogus());
                        HttpURLConnection httpConn2 = (HttpURLConnection) url2.openConnection();
                        httpConn2.setRequestMethod("GET");
                        httpConn2.setRequestProperty("authority", "www.douyin.com");
                        httpConn2.setRequestProperty("accept", "application/json, text/plain, */*");
                        httpConn2.setRequestProperty("accept-language", "zh-CN,zh;q=0.9");
                        httpConn2.setRequestProperty("cookie", cookie.getCookies());
                        httpConn2.setRequestProperty("referer", "https://www.douyin.com/video/"+videoId);
                        httpConn2.setRequestProperty("sec-ch-ua", "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"");
                        httpConn2.setRequestProperty("sec-ch-ua-mobile", "?0");
                        httpConn2.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
                        httpConn2.setRequestProperty("sec-fetch-dest", "empty");
                        httpConn2.setRequestProperty("sec-fetch-mode", "cors");
                        httpConn2.setRequestProperty("sec-fetch-site", "same-origin");
                        httpConn2.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
                        int responseCode2 = httpConn2.getResponseCode();
                        // 读取响应内容
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(httpConn2.getInputStream()));
                        String line2;
                        StringBuilder response2 = new StringBuilder();
                        while ((line2 = reader2.readLine()) != null) {
                            response2.append(line2);
                        }
                        reader2.close();
                        System.out.println("Response Code: " + responseCode2);
                        JSONObject jsonObject2 =  JSON.parseObject(response2.toString());
                        Video video1 = videoMapper.selectOne(new QueryWrapper<Video>().eq("task_id", task.getId()).eq("video_link", "https://www.douyin.com/video/" + videoId));
                        if (video1==null||video1.getSize()==0){
                            //保存视频
                            video.setTaskId(task.getId());
                            video.setVideoLink("https://www.douyin.com/video/"+videoId);
                            video.setDiggingTime(LocalDateTime.now());
                            video.setPhone(ShiroKit.getUser().getUsername());
                            video.setTaskName(task.getName());
                            video.setCreateTime(LocalDateTime.now());
                            String desc = (String) data.getJSONObject("aweme_info").get("desc");
                            video.setLabel(desc);
                            videoMapper.insert(video);
                        }
                        JSONArray comments = jsonObject2.getJSONArray("comments");
                        for (Object pl:comments){
                            try{
                                JSONObject pljsonObject = JSONObject.parseObject(pl.toString());
                                //评论的内容
                                String text = (String) pljsonObject.get("text");
                                //评论的时间
                                Object timeStamp =pljsonObject.get("create_time")+"000";//获取当前时间戳
                                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));// 时间戳转换成时间
                                //客户网名
                                String nickname = (String) pljsonObject.getJSONObject("user").get("nickname");
                                //客户主页链接
                                Object o2 = pljsonObject.getJSONObject("user").get("sec_uid");
                                JSONArray photo = pljsonObject.getJSONObject("user").getJSONObject("avatar_larger").getJSONArray("url_list");
                                //客户头像
                                Object avatar = photo.get(0);
                                //取出关键字
                                String siftKeywords = task.getSiftKeywords();
                                String[] siftKeywordsList = siftKeywords.split(",");
                                boolean siftContains = false;
                                //获取命中关键词
                                String keyword = null;
                                for (String s : siftKeywordsList) {
                                    siftContains = text.contains(s);
                                    if (siftContains) {
                                        keyword = s;
                                        break;
                                    }
                                }
                                if (siftContains && task.getShieldKeywords() == null || siftContains && task.getShieldKeywords().equals("")) {
                                    IntentionCust intentionCust=new IntentionCust();
                                    intentionCust.setTaskId(task.getId());
                                    intentionCust.setVideoLink("https://www.douyin.com/video/"+videoId);
                                    intentionCust.setNickname(nickname);
                                    intentionCust.setContent(text);
                                    intentionCust.setPhone(ShiroKit.getUser().getUsername());
                                    intentionCust.setHitKeyword(null);
                                    intentionCust.setConsuTime(sd);
                                    intentionCust.setHitKeyword(keyword);
                                    intentionCust.setAnalyseTime(LocalDateTime.now());
                                    intentionCust.setCreateTime(LocalDateTime.now());
                                    intentionCust.setCustHome("https://www.douyin.com/user/"+o2);
                                    intentionCust.setTaskName(task.getName());
                                    intentionCust.setLabel(video.getLabel());
                                    intentionCust.setVideoId(video.getId());
                                    intentionCust.setCustAvatar((String) avatar);
                                    intentionCustMapper.insert(intentionCust);
                                }
                                if (siftContains && task.getShieldKeywords() != null && !task.getShieldKeywords().equals("")) {
                                    //取出屏蔽关键词
                                    String shieldKeywords = task.getShieldKeywords();
                                    String[] shieldKeywordsList = shieldKeywords.split(",");
                                    boolean shieldContains = false;
                                    for (String s : shieldKeywordsList) {
                                        shieldContains = text.contains(s);
                                        if (shieldContains) {
                                            break;
                                        }
                                    }
                                    if (!shieldContains) {
                                        IntentionCust intentionCust=new IntentionCust();
                                        intentionCust.setTaskId(task.getId());
                                        intentionCust.setVideoLink("https://www.douyin.com/video/"+videoId);
                                        intentionCust.setNickname(nickname);
                                        intentionCust.setContent(text);
                                        intentionCust.setPhone(ShiroKit.getUser().getUsername());
                                        intentionCust.setHitKeyword(null);
                                        intentionCust.setConsuTime(sd);
                                        intentionCust.setHitKeyword(keyword);
                                        intentionCust.setAnalyseTime(LocalDateTime.now());
                                        intentionCust.setCreateTime(LocalDateTime.now());
                                        intentionCust.setCustHome("https://www.douyin.com/user/"+o2);
                                        intentionCust.setTaskName(task.getName());
                                        intentionCust.setLabel(video.getLabel());
                                        intentionCust.setVideoId(video.getId());
                                        intentionCust.setCustAvatar((String) avatar);
                                        intentionCustMapper.insert(intentionCust);
                                    }
                                }
                                //获得意向数量的客户 跳出循环
                                Integer count = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
                                if (Objects.equals(count, task.getCustNum())) {
                                    break;
                                }
                            }catch (Exception e){
                                System.out.println(e);
                            }
                        }
                        plCursor = (int) jsonObject2.get("cursor");
                        plhasMore = (int) jsonObject2.get("has_more");
                        Integer count= intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
                        if (Objects.equals(count, task.getCustNum())){
                            break;
                        }
                    }
                    Integer integer = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("video_id", video.getId()));
                    video.setVideoCustNum(integer);
                    videoMapper.updateById(video);
                }catch (Exception e){
                    System.out.println("异常跳出当前循环 继续执行");
                }

                Integer count= intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
                if (Objects.equals(count, task.getCustNum())){
                    break;
                }
            }
            //执行结束后查一下客户库数量是否满足意向客户数量
            a = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
            //翻页的值
            Object cursor = jsonObject.get("cursor");
            pcursor= (int) cursor;
            System.out.println(pcursor);
            //是否还存在下一页 1存在 0不存在
            Object has_more = jsonObject.get("has_more");
            System.out.println(has_more.toString());
            hasMore = (Integer) has_more;
            //获取当前时间戳
            LocalDateTime now2 = LocalDateTime.now();
            time= now2.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        }

        Integer count = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
        task.setUseCustNum(count);
        taskMapper.updateById(task);
        //减掉account的额度
        //获取当前用户信息
        Account account = accountMapper.selectById(task.getAccountId());
        if (account.getCustNum() != null && account.getCustNum() > 0) {
            Account account2 = accountMapper.selectById(task.getAccountId());
            Integer i = account2.getCustNum() - count;
            account2.setCustNum(i);
            accountMapper.updateById(account2);
        }
        return R.ok();
    }
}
